package com.dgut.test3.controller;

import com.dgut.test3.bean.RegionStats;
import com.dgut.test3.service.GetData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;


@RestController
public class ShowData {
    @Autowired
    GetData getData = new GetData();

    @RequestMapping(value = "/index")
    public ModelAndView showData() throws IOException {
        ModelAndView modelAndView = new ModelAndView();
        List<RegionStats> data = getData.fetchCoronaVirusData();

        int allPeople = 0;
        int allNewPeople = 0;

        for(RegionStats stats:data){
            allPeople = allPeople + stats.getLatestTotalCases();
            allNewPeople = allNewPeople + stats.getDiffFromPrevDay();
        }
        modelAndView.addObject("data",data);
        modelAndView.addObject("allPeople",allPeople);
        modelAndView.addObject("allNewPeople",allNewPeople);
        modelAndView.setViewName("indexPage");

        return modelAndView;
    }

    @PostMapping(value = "/select")
    public ModelAndView showSelectData(String state,String country) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        List<RegionStats> data = getData.selectCVData(state, country);
        List<RegionStats> data2 = getData.fetchCoronaVirusData();
        int allPeople = 0;
        int allNewPeople = 0;

        for(RegionStats stats:data2){
            allPeople = allPeople + stats.getLatestTotalCases();
            allNewPeople = allNewPeople + stats.getDiffFromPrevDay();
        }

        modelAndView.addObject("data",data);
        modelAndView.addObject("allPeople",allPeople);
        modelAndView.addObject("allNewPeople",allNewPeople);
        modelAndView.setViewName("indexPage");
        return modelAndView;
    }

}
